home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------
-
- log.c
-
- This module handles logging.
-
- Copyright © 1994, Northwestern University.
-
- ----------------------------------------------------------------------------*/
-
- #include <Packages.h>
- #include <stdio.h>
- #include <string.h>
-
- #include "glob.h"
- #include "log.h"
- #include "dialog.h"
- #include "strutil.h"
- #include "fileutil.h"
- #include "resutil.h"
- #include "ic.h"
-
-
-
- static short gRefNum = 0;
-
-
-
- /*----------------------------------------------------------------------------
- PutString
-
- Write a string to the log file.
-
- Entry: str = string to write.
- ----------------------------------------------------------------------------*/
-
- static void PutString (char *str)
- {
- char line[513];
- long len;
-
- sprintf(line, "%s\r", str);
- len = strlen(line);
- MyFSWriteNoCache(gRefNum, &len, line, nil);
- }
-
-
-
- /*----------------------------------------------------------------------------
- OpenLogFile
-
- Open the log file.
- ----------------------------------------------------------------------------*/
-
- void OpenLogFile (void)
- {
- FCBPBRec pBlock;
- FSSpec logFile;
- OSErr err = noErr;
- Str255 versStr, fileName;
- CStr255 str, fmt;
- Boolean empty;
-
- MyICReadSharedPrefs(kICeditorHelper);
-
- if (gRefNum != 0) return;
- pBlock.ioNamePtr = nil;
- pBlock.ioVRefNum = 0;
- pBlock.ioRefNum = LMGetCurApRefNum();
- pBlock.ioFCBIndx = 0;
- err = PBGetFCBInfo(&pBlock, false);
- if (err != noErr) goto exit;
- GetPString(kStrLogFileName, fileName);
- err = FSMakeFSSpec(pBlock.ioFCBVRefNum, pBlock.ioFCBParID,
- fileName, &logFile);
- if (err != noErr && err != fnfErr) goto exit;
-
- err = OpenDataForkWriteCreateIfMissing(&logFile, gPrefs.savedArtCreator, 'TEXT',
- smSystemScript, false, &gRefNum, &empty);
- if (err != noErr) goto exit;
-
- PutString("");
- PutString("------------------------------------------------------------------------------");
- PutString("");
- GetCString(kStrLogOpenMsg, str);
- PutString(str);
- err = GetVersionString(versStr);
- if (err != noErr) goto exit;
- GetCString(kStrNewsWatcherVersion, fmt);
- p2cstr(versStr);
- sprintf(str, fmt, versStr);
- PutString(str);
- GetCString(kStrLogLegend, str);
- PutString(str);
- PutString("");
- return;
-
- exit:
-
- if (gRefNum != 0) MyFSClose(gRefNum, nil);
- gRefNum = 0;
- ErrorMessageNumber(kStrLogCantOpen);
- }
-
-
-
- /*----------------------------------------------------------------------------
- CloseLogFile
-
- Close the log file.
- ----------------------------------------------------------------------------*/
-
- void CloseLogFile (void)
- {
- CStr255 str;
-
- if (gRefNum == 0) return;
- PutString("");
- GetCString(kStrLogClosed, str);
- PutString(str);
- MyFSClose(gRefNum, nil);
- gRefNum = 0;
- }
-
-
-
- /*----------------------------------------------------------------------------
- Log
-
- Log a server command or response.
-
- Entry: command = true if command, false if response.
- ipAddr = IP address of server.
- str = command or response.
- ----------------------------------------------------------------------------*/
-
- void Log (Boolean command, unsigned long ipAddr, char *str)
- {
- char line[512];
- char c;
- short o1, o2, o3, o4;
-
- if (gRefNum == 0) return;
- c = command ? 'C' : 'R';
- o1 = (ipAddr >> 24) & 0xff;
- o2 = (ipAddr >> 16) & 0xff;
- o3 = (ipAddr >> 8) & 0xff;
- o4 = ipAddr & 0xff;
- if (command && MyStrNEqual(str, "PASS", 4)) {
- sprintf(line, "C %d.%d.%d.%d PASS *******", o1, o2, o3, o4);
- } else if (command && MyStrNEqual(str, "AUTHINFO PASS", 13)) {
- sprintf(line, "C %d.%d.%d.%d AUTHINFO PASS *******", o1, o2, o3, o4);
- } else {
- sprintf(line, "%c %d.%d.%d.%d %s", c, o1, o2, o3, o4, str);
- }
- PutString(line);
- }